#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPalette>
#include <QFormLayout>
#include <QSizePolicy>
#include <QMouseEvent>
#include <QFileDialog>
#include "dialogapropos.h"
#include "dialoghelp.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    _group = new QActionGroup( this );
    ui->actionPetit->setCheckable(true);
    ui->actionMoyen->setCheckable(true);
    ui->actionGrand->setCheckable(true);
    ui->actionGrand->setActionGroup(_group);
    ui->actionMoyen->setActionGroup(_group);
    ui->actionPetit->setActionGroup(_group);
    colorPicker.setParent(this, Qt::Popup);
    colorPicker_2.setParent(this, Qt::Popup);
    colorPicker.setVisible(false);
    colorPicker_2.setVisible(false);
    connect(colorPicker.getColorPicker(),SIGNAL(colorChanged(QColor)),SLOT(colorWidgetForground_ColorChange(QColor)));
    connect(colorPicker_2.getColorPicker(),SIGNAL(colorChanged(QColor)),SLOT(colorWidgetBackground_ColorChange(QColor)));
    connect(ui->fontComboBox,SIGNAL(currentFontChanged(QFont)),SLOT(police_currentFontChanged(QFont)));
    connect(ui->pushButton_Gras,SIGNAL(clicked(bool)),SLOT(gras_triggered(bool)));
    connect(ui->pushButton_Italique,SIGNAL(clicked(bool)),SLOT(italique_triggered(bool)));
    connect(ui->pushButton_Underline,SIGNAL(clicked(bool)),SLOT(underlinetriggered(bool)));
    connect(ui->pushButton_Upper,SIGNAL(clicked(bool)),SLOT(up_triggered(bool)));
    connect(ui->pushButton_Lower,SIGNAL(clicked(bool)),SLOT(down_triggered(bool)));
    connect(ui->comboBox_Weight,SIGNAL(currentIndexChanged(QString)),SLOT(weight_change(QString)));
    connect(ui->actionAnnuler,SIGNAL(triggered()),SLOT(undo()));
    connect(ui->actionRevenir,SIGNAL(triggered()),SLOT(redo()));
    connect(ui->actionEnregistrer,SIGNAL(triggered()),SLOT(saveConfig()));
    connect(ui->actionOuvrir,SIGNAL(triggered()),SLOT(openConfig()));
    connect(ui->actionOuvrir_un_texte,SIGNAL(triggered()),SLOT(openText()));
    connect(ui->actionEnregistrer_le_texte,SIGNAL(triggered()),SLOT(saveText()));
    connect(ui->lineEdit,SIGNAL(textChanged(QString)),SLOT(textChanged()));
    connect(ui->actionCopier,SIGNAL(triggered()),SLOT(copier()));
    connect(ui->actionCouper,SIGNAL(triggered()),SLOT(couper()));
    connect(ui->actionColler,SIGNAL(triggered()),SLOT(coller()));
    ui->fontComboBox->setCurrentFont(ui->fontComboBox->currentFont());
    ui->comboBox_Weight->setCurrentIndex(10);
    colorPicker.getColorPicker()->setColor(this->palette().text().color());
    colorPicker_2.getColorPicker()->setColor(this->palette().light().color());
    connect(ui->pushButton,SIGNAL(clicked()),SLOT(btnForground()));
    connect(ui->pushButton_2,SIGNAL(clicked()),SLOT(btnBackground()));
    ui->centralWidget->installEventFilter(this);
    ui->lineEdit->setFocus();
    ui->statusBar->showMessage("BERNARD Florian & COLLOMBET Guillaume");
    this->setWindowFlags(Qt::WindowContextHelpButtonHint);

}

void MainWindow::btnBackground()
{
    bool visible = colorPicker_2.isVisible();
    QPoint pos = ui->pushButton_2->pos();
    colorPicker_2.move(mapToGlobal(QPoint(pos.rx(), pos.ry() + ui->pushButton_2->height())));
    colorPicker_2.setVisible(!visible);
    colorPicker_2.resize(ui->pushButton_2->width() ,ui->pushButton_2->width());
}

void MainWindow::btnForground()
{
    QPoint pos = ui->pushButton->pos();
    colorPicker.move(mapToGlobal(QPoint(pos.rx(), pos.ry() + ui->pushButton->height())));
    bool visible = colorPicker.isVisible();
    colorPicker.setVisible(!visible);
    colorPicker.resize(ui->pushButton->width() ,ui->pushButton->width());
}

void MainWindow::colorWidgetForground_ColorChange(const QColor &color)
{
    QTextCursor c = ui->textEdit->textCursor();
    ui->textEdit->selectAll();
    ui->textEdit->setTextColor(color);
    ui->textEdit->setTextCursor(c);
}

void MainWindow::colorWidgetBackground_ColorChange(const QColor &color)
{
    QTextCursor c = ui->textEdit->textCursor();
    ui->textEdit->selectAll();
    ui->textEdit->setTextBackgroundColor(color);
    ui->textEdit->setTextCursor(c);
}

void MainWindow::police_currentFontChanged(const QFont& font)
{
    QTextCursor c = ui->textEdit->textCursor();
    ui->textEdit->selectAll();
    ui->textEdit->setFontFamily(font.family());
    ui->textEdit->setTextCursor(c);
}

void MainWindow::gras_triggered(bool b)
{
    QTextCursor c = ui->textEdit->textCursor();
    ui->textEdit->selectAll();
    ui->textEdit->setFontWeight(b?QFont::Bold:QFont::Normal);
    ui->textEdit->setTextCursor(c);
}

void MainWindow::italique_triggered(bool b)
{
    QTextCursor c = ui->textEdit->textCursor();
    ui->textEdit->selectAll();
    ui->textEdit->setFontItalic(b);
    ui->textEdit->setTextCursor(c);
}

void MainWindow::underlinetriggered(bool b)
{
    QTextCursor c = ui->textEdit->textCursor();
    ui->textEdit->selectAll();
    ui->textEdit->setFontUnderline(b);
    ui->textEdit->setTextCursor(c);
}

void MainWindow::up_triggered(bool)
{
    ui->comboBox_Weight->setCurrentIndex(ui->comboBox_Weight->currentIndex() + 1);
}

void MainWindow::down_triggered(bool)
{
    ui->comboBox_Weight->setCurrentIndex(ui->comboBox_Weight->currentIndex() - 1);
}

void MainWindow::weight_change(const QString& value)
{
    QTextCursor c = ui->textEdit->textCursor();
    ui->textEdit->selectAll();
    ui->textEdit->setFontPointSize(value.toUInt());
    ui->textEdit->setTextCursor(c);
    if(ui->comboBox_Weight->currentIndex() == (ui->comboBox_Weight->count() - 1))
        ui->pushButton_Upper->setEnabled(false);
    if(ui->comboBox_Weight->currentIndex() > 0)
        ui->pushButton_Lower->setEnabled(true);
    if(ui->comboBox_Weight->currentIndex() == 0)
        ui->pushButton_Lower->setEnabled(false);
    if(ui->comboBox_Weight->currentIndex() < (ui->comboBox_Weight->count() - 1))
        ui->pushButton_Upper->setEnabled(true);
}

void MainWindow::saveConfig()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Enregistrer la mise en forme"), "", tr("Configuration file (*.conf)"));
    QString text;
    text.append(ui->pushButton_Gras->isChecked()?"1\n":"0\n");
    text.append(ui->pushButton_Italique->isChecked()?"1\n":"0\n");
    text.append(ui->pushButton_Underline->isChecked()?"1\n":"0\n");
    text.append(QString().setNum(ui->comboBox_Weight->currentIndex()) + "\n");
    text.append(ui->fontComboBox->currentFont().family() + "\n");
    text.append(QString().setNum(colorPicker.getColorPicker()->color().red()) + ";" +
                QString().setNum(colorPicker.getColorPicker()->color().green()) + ";" +
                QString().setNum(colorPicker.getColorPicker()->color().blue()) + "\n");
    text.append(QString().setNum(colorPicker_2.getColorPicker()->color().red()) + ";" +
                QString().setNum(colorPicker_2.getColorPicker()->color().green()) + ";" +
                QString().setNum(colorPicker_2.getColorPicker()->color().blue()) + "\n");
    QFile file(fileName);
    if(file.open(QIODevice::WriteOnly))
    {
        file.write(text.toAscii());
        file.close();
    }
}

void MainWindow::openConfig()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Ouvrir une configuration de mise en forme"), "", tr("Configuration file (*.conf)"));
    QFile file(fileName);
    if(file.open(QIODevice::ReadOnly))
    {
             QByteArray line;
             line = file.readLine().replace("\n","");
             ui->pushButton_Gras->setChecked((line == "1") ? true : false);
             line = file.readLine().replace("\n","");
             ui->pushButton_Italique->setChecked((line == "1") ? true : false);
             line = file.readLine().replace("\n","");
             ui->pushButton_Underline->setChecked((line == "1") ? true : false);
             line = file.readLine().replace("\n","");
             ui->comboBox_Weight->setCurrentIndex(line.toInt());
             line = file.readLine().replace("\n","");
             ui->fontComboBox->setCurrentFont(QFont(line));
             line = file.readLine().replace("\n","");
             QList<QByteArray> list(line.split(';'));
             colorPicker.getColorPicker()->setColor(QColor(QString(list[0]).toInt(),QString(list[1]).toInt(),QString(list[2]).toInt()));
             line = file.readLine().replace("\n","");
             QList<QByteArray> list2(line.split(';'));
             colorPicker_2.getColorPicker()->setColor(QColor(QString(list2[0]).toInt(),QString(list2[1]).toInt(),QString(list2[2]).toInt()));

        file.close();
    }
}

void MainWindow::openText()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Ouvrir un texte"), "", tr("Fichier texte (*.txt)"));
    QFile file(fileName);
    if(file.open(QIODevice::ReadOnly))
    {
        QString texte = file.readAll();
        ui->lineEdit->setText(texte);
        file.close();
    }
}

void MainWindow::saveText()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Enregistrer le texte"), "", tr("Fichier texte (*.txt)"));
    QFile file(fileName);
    if(file.open(QIODevice::WriteOnly))
    {
        file.write(ui->lineEdit->text().toAscii());
        file.close();
    }
}

void MainWindow::textChanged()
{
    QString text  =ui->lineEdit->text();
    ui->textEdit->selectAll();
    ui->textEdit->setText(text);

    gras_triggered(ui->pushButton_Gras->isChecked());
    italique_triggered(ui->pushButton_Italique->isChecked());
    underlinetriggered(ui->pushButton_Underline->isChecked());
    weight_change( ui->comboBox_Weight->currentText());
    police_currentFontChanged(ui->fontComboBox->currentFont());
    colorWidgetForground_ColorChange(colorPicker.getColorPicker()->color());
    colorWidgetBackground_ColorChange(colorPicker_2.getColorPicker()->color());
}

void MainWindow::undo()
{
    ui->textEdit->undo();
}

void MainWindow::redo()
{
    ui->textEdit->redo();
}

void MainWindow::copier()
{
    ui->textEdit->copy();
}

void MainWindow::couper()
{
    ui->textEdit->cut();
}

void MainWindow::coller()
{
    ui->textEdit->paste();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionA_propos_de_triggered()
{
    DialogAPropos dial(this);
    dial.exec();
}

void MainWindow::on_actionAide_triggered()
{
    DialogHelp dial(this);
    dial.setModal(false);
    dial.exec();
}
